module.exports = (express, app) => {
  const Carousel = require('../../models/Carousel')
  const router = express.Router()

  // 获取全部数据
  router.get('/', async (req, res) => {
    await Carousel.find().exec((err, data) => {
      err && res.send({ code: 0, msg: err.message })
      res.send(data)
    })
  })

  /**
   * 添加
   * @param {String} imgURL 
   */
  router.post('/', async (req, res) => {
    await Carousel.create(req.body, (err) => {
      err && res.send({ code: 0, msg: err.message })
      res.send({ code: 1 })
    })
  })


  /**
   * 删除
   * @param {String} id id字段
   */
  router.delete('/:id', async (req, res) => {
    await Carousel.findByIdAndDelete(req.params.id).exec((err, data) => {
      err && res.send({ code: 0, msg: err.message })
      res.send({ code: 1 })
    })
  })

  /**
   * 批量删除
   * @param {String} idArr 多个id使用,隔开
   */
  router.delete('/deleteMany/:idArr', async (req, res) => {
    const newIdArr = req.params.idArr.split(","); //格式化为我们需要的
    await Carousel.deleteMany({ _id: { $in: newIdArr } }).exec((err, data) => {
      err && res.send({ code: 0, msg: err.message })
      res.send({ code: 1 })
    })
  })

  /** 
   * 更新
   * @param {String} id id字段
   */
  router.put('/:id', async (req, res) => {
    await Carousel.findByIdAndUpdate(req.params.id, req.body).exec((err, data) => {
      err && res.send({ code: 0, msg: err.message })
      res.send({ code: 1 })
    })
  })

  app.use('/admin/api/home/carousel', router)
}